package com.bonc.common.utils;

import com.alibaba.fastjson.JSONObject;
import com.bonc.modules.business.entity.TbCommonParamInfoEntity;
import org.apache.commons.lang3.StringUtils;

import java.io.UnsupportedEncodingException;
import java.lang.reflect.Field;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author cxy
 * @Title: ResolveParamUtils
 * @Description: 解析接口公共参数类
 * @date 2021年11月2日14:06:04
 */
public class ResolveParamUtils {
    /**
     * <p>
     * 解析公共参数
     * </P>
     * @author cxy
     * @date 2021年11月2日14:07:23
     */
    public static Map<String, Object> getAllParam(JSONObject content) {
        String accessToken = "2022021611194318072e4e2f524c3da560ac5f5f812cad-6229-1642389583";
        Calendar calendar= Calendar.getInstance();
        SimpleDateFormat dateFormat= new SimpleDateFormat("yyyyMMddHHmmss");
        SimpleDateFormat dateFormat1= new SimpleDateFormat("yyyyMMdd");
        String cutOffDay = dateFormat1.format(calendar.getTime());
        String timeStamp = dateFormat.format(calendar.getTime());
        String transIDO = timeStamp + System.currentTimeMillis();
        String sessionID = transIDO;
        String sign = "";
        String envFlag = "1";
        String version = "1.0.1";
        String signMethod = "MD5";
        String domain = "CSVC";
        String routeType = "00";
        String routeValue = "998";
        String busType = "CSVC";

        Map<String,Object> maps = new TreeMap<String,Object>();
        Iterator iterator = content.entrySet().iterator();
        while(iterator.hasNext())
        {
            Map.Entry<String,Object> entry = (Map.Entry<String,Object>) iterator.next();
            maps.put(entry.getKey(),entry.getValue());
        }
        maps.entrySet();

        //总体：所有研究对象的全体
        Map<String, Object> param = new TreeMap<>();
        param.put("cutOffDay", cutOffDay);
        param.put("timeStamp", timeStamp);
        param.put("transIDO", transIDO);
        param.put("sessionID", sessionID);
        param.put("envFlag", envFlag);
        param.put("accessToken", accessToken);
        param.put("version", version);
        param.put("domain", domain);
        param.put("signMethod", signMethod);
        param.put("routeType", routeType);
        param.put("routeValue", routeValue);
        param.put("busType", busType);
        param.put("content", JSONObject.toJSONString(maps));

        //字段参数升序排序
        param.entrySet();
        String appSecret = "r1v7w0E84j";//appSecret
        StringBuilder builder = new StringBuilder(appSecret);
        int i = 0;
        //将参数通过keyvalue的方式进行拼接  key传空值默认不拼接
        for (String key : param.keySet()) {
            Object value = param.get(key);
            if (value == null) { // 过滤空的key
                continue;
            }
            builder.append(key).append(value.toString().trim());
            i++;
        }
        builder.append(appSecret);
        System.out.println(builder.toString());
        try {
            sign = SignUtil.checkMD5(builder.toString());
            param.put("sign", sign);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        return param;
    }

    public static String analysisAllParam(TbCommonParamInfoEntity tbCommonParamValue) throws IllegalAccessException, UnsupportedEncodingException, NoSuchAlgorithmException {
        Map<String,Object> param = new TreeMap<String,Object>();

        String appSecret = "YRZ803Rxvx";//appSecret
        Field[] fields = tbCommonParamValue.getClass().getDeclaredFields();
        for(Field field : fields){
            field.setAccessible(true);
            String name = new String(field.getName());
            if(!name.equals("sign") && !name.equals(("userPartyID"))){
                //param.put(name, URLEncoder.encode(field.get(tbCommonParamValue).toString(), "UTF-8"));
                param.put(name, field.get(tbCommonParamValue));
            }else{
                //sign = field.get(tbCommonParamValue).toString();
            }
        }

        param.entrySet();
        StringBuilder builder = new StringBuilder(appSecret);
        int i = 0;
        //将参数通过keyvalue的方式进行拼接  key传空值默认不拼接
        for (String key : param.keySet()) {
            Object value = param.get(key);
            if (value == null) { // 过滤空的key
                continue;
            }
            builder.append(key).append(value.toString().trim());
            i++;
        }
        builder.append(appSecret);
        //String signjy = AppUtils.MD5_32bit(builder.toString());
        String signjy = SignUtil.checkMD5(builder.toString());
        return signjy;
    }

    public static void main(String[] args) {

    }
}
